<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="layui/css/layui.css">
</head>
<body>
   <div class="layui-fluid">
      <table class="layui-hide" id="test" lay-filter="test"></table>
  </div>
<script id="toolbarDemo" type="text/html">
 <!-- 
    <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
    <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
-->
    <button class="layui-btn layui-btn-sm" lay-event="add">添加人才</button>
    <div class="layui-inline">
     <div class="layui-input-inline" style="width:150px;height:38px;">
       <input type="text" name="likeName" id="likeName" required lay-verify="required" placeholder="请输入姓名"  class="layui-input">
     </div>
     <div class="layui-inline">
       <button class="layui-btn layui-btn-sm"  data-type="reload" id="search"><i class="layui-icon">&#xe615;</i>搜索</button>
     </div>  
  </div>
  
</script>

<script id="barDemo" type="text/html">
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/html" id="editLayer">
    <!-- 编辑模态框 -->
    <div>
     <form action="/editTalent" class="layui-form" id="editForm">
      <div class="layui-form-item">
       <label class="layui-form-label">id</label>
       <div class="layui-input-inline">
          <input class="layui-input" type="text" name="id" id="id"  readonly="readonly">
       </div>   
     </div>
     <div class="layui-form-item">
       <label class="layui-form-label">姓名</label>
       <div class="layui-input-inline">
          <input class="layui-input" type="text" name="name" id="name">
       </div>   
     </div>
     <div class="layui-form-item">
       <label class="layui-form-label">年龄</label>
       <div class="layui-input-inline">
          <input class="layui-input" type="text" name="age" id="age">
       </div>   
     </div>
     <div class="layui-form-item">
       <label class="layui-form-label">应聘职位</label>
       <div class="layui-input-inline">
          <input class="layui-input" type="text" name="huntPosition" id="huntPosition">
       </div>   
     </div>
     <div class="layui-form-item">
       <label class="layui-form-label">期望薪资</label>
       <div class="layui-input-inline">
          <input class="layui-input" type="text" name="askSlary" id="askSlary">
       </div>   
     </div>
     <div class="layui-form-item">
       <label class="layui-form-label">技能</label> 
         <div class="layui-input-inline">
           <input class="layui-input" type="text" name="skill" id="skill">
        </div>          
     </div>
     <div class="layui-form-item">
       <label class="layui-form-label">电话</label> 
         <div class="layui-input-inline">
           <input class="layui-input" type="text" name="phone" id="phone">
        </div>          
     </div>
     <div class="layui-form-item">
       <label class="layui-form-label">毕业学校</label> 
         <div class="layui-input-inline">
           <input class="layui-input" type="text" name="school" id="school">
        </div>          
     </div>
     <div class="layui-form-item">
       <label class="layui-form-label">专业</label> 
         <div class="layui-input-inline">
           <input class="layui-input" type="major" name="skill" id="major">
        </div>          
     </div>
     <div class="layui-form-item">
       <label class="layui-form-label">面试时间</label> 
         <div class="layui-input-inline">
           <input class="layui-input" type="text" name="interviewTime" id="interviewTime">
        </div>          
     </div>
     </form> 
    </div>
</script>

<script type="text/html" id="addLayer">
    <!-- 添加模态框 -->
   <div>
     <form action="/addTalent" class="layui-form" id="addForm">

     <div class="layui-form-item">
       <label class="layui-form-label">姓名</label>
       <div class="layui-input-inline">
          <input class="layui-input" type="text" name="name" id="name">
       </div>   
     </div>
     <div class="layui-form-item">
       <label class="layui-form-label">年龄</label>
       <div class="layui-input-inline">
          <input class="layui-input" type="text" name="age" id="age">
       </div>   
     </div>
     <div class="layui-form-item">
       <label class="layui-form-label">应聘职位</label>
       <div class="layui-input-inline">
          <input class="layui-input" type="text" name="huntPosition" id="huntPosition">
       </div>   
     </div>
     <div class="layui-form-item">
       <label class="layui-form-label">期望薪资</label>
       <div class="layui-input-inline">
          <input class="layui-input" type="text" name="askSlary" id="askSlary">
       </div>   
     </div>
     <div class="layui-form-item">
       <label class="layui-form-label">技能</label> 
         <div class="layui-input-inline">
           <input class="layui-input" type="text" name="skill" id="skill">
        </div>          
     </div>
     <div class="layui-form-item">
       <label class="layui-form-label">电话</label> 
         <div class="layui-input-inline">
           <input class="layui-input" type="text" name="phone" id="phone">
        </div>          
     </div>
     <div class="layui-form-item">
       <label class="layui-form-label">毕业学校</label> 
         <div class="layui-input-inline">
           <input class="layui-input" type="text" name="school" id="school">
        </div>          
     </div>
     <div class="layui-form-item">
       <label class="layui-form-label">专业</label> 
         <div class="layui-input-inline">
           <input class="layui-input" type="major" name="skill" id="major">
        </div>          
     </div>
     <div class="layui-form-item">
       <label class="layui-form-label">面试时间</label> 
         <div class="layui-input-inline">
           <input class="layui-input" type="text" name="interviewTime" id="interviewTime">
        </div>          
     </div>
     </form> 
    </div>
</script>

<script src="layui/layui.js"></script> 

<script th:inline="none">
  layui.use('table',function(){
	  var table=layui.table;
	  var $=layui.jquery;
	  table.render({
		 elem:'#test',
		 url:'/getTalentBank',
		 method:'post',
	     toolbar:'#toolbarDemo',
	     defaultToolbar:['filter','exports','print',{title:'提示',layEvent:'LAYTABLE_TIPS',icon:'layui-icon-tips'}],
	     title:'用户数据表',
	   //  response:{statusCode:200},
	     cols:[[
	    	 {type:'checkbox',fixed:'left'}
	    	 ,{field:'id',title:'ID',fixed:'left',unresize:true,sort:true,width:80}
	    	 ,{field:'name',title:'姓名',edit:'text',width:120}
	    	 ,{field:'age', title:'年龄', edit: 'text', sort: true,width:80}
	    	 ,{field:'huntPosition', title:'应聘职位',width:120}
	    	 ,{field:'askSlary', title:'期望薪资',width:110}
	    	 ,{field:'skill', title:'技能',width:120}
	    	 ,{field:'phone', title:'电话',width:120}
	    	 ,{field:'school', title:'毕业学校',width:150}
	    	 ,{field:'major', title:'专业',width:180}
	    	 ,{field:'interviewTime', title:'面试时间',width:120},
	    	 {fixed: 'right', title:'操作', toolbar: '#barDemo',width:120}
	     ]],
	     id:'testreload',
	     page:true
	  });
	  
	  //表格数据重载
	  var active={
			  reload:function(){
				  var demoReload=$('#likeName');
				  table.reload('testreload',{
					  page:{
						  curr:1
					  },
					  where:{
						  //参数name的名称要与控制器中的一致
						  name:demoReload.val()
					  },
					  url:'/searchTalent',
					  method:'post'
				  });
			  }
	  };
	  
	  //搜索按钮事件
	  
	  $("#search").on('click',function(){
		  var type=$(this).data('type');
		  active[type]?active[type].call(this):'';
	  });
	  
	  table.on('toolbar(test)',function(obj){
		  var checkStatus=table.checkStatus(obj.config.id);
		  switch(obj.event){
		  case 'add':
			 // var data=checkStatus.data;
			//  layer.alert(JSON.stringify(data));
			  layui.use('form',function(){
				  var form =layui.form;
				  var $=layui.jquery;
				//  var id1=$("#id").val();
				//  var account=$("#account").val();
				
				  var index=layer.open({
					  formType:1,
					  title:'添加人才库',
					  area:['450px','480px'],
					  content:$('#addLayer').html(),
					  btn:['确认','取消'],
					  yes:function(index,layero){
						 // var id=$("#id").val();
						     var name=$("#name").val();
						     var age=$("#age").val();
						     var huntPosition=$("#huntPosition").val();
						     var askSlary=$("#askSlary").val();
						     var skill=$("#skill").val();
						     var interviewTime=$("#interviewTime").val();
						  $.ajax({
							  url:'/addTalent',
							  method:'post',
							  contentType:"application/json;charset=utf-8",
							  data:JSON.stringify({name:name,age:age,huntPosition:huntPosition,askSlary:askSlary,skill:skill,interviewTime:interviewTime}),
							  success:function(data){
								  if(data){
								  layer.msg("添加成功");
								  window.setTimeout(function(){window.location.reload();},500)
								  }else{layer.msg("添加失败");}
							  },
							  error:function(data){
								  layer.msg("添加失败");
								  //layer.msg(account1);
							  }
						  });
					  },
					  btn2:function(index){layer.close(index);},
					  success:function(index,layero){
						  layui.use('laydate',function(){
							  var laydate=layui.laydate;
							  laydate.render({
								  elem:'#interviewTime',
								  type:'date',
							  });
						  });
						//  form.render();
					  },
				  });
			  });
		  break;
		  case 'getCheckLength':
			  var data=checkStatus.data;
			  layer.msg('选中了：'+data.length+'个');
		  break;
		  case 'isAll':
			  layer.msg(checkStatus.isAll?'全选':'未全选');
		  break;
		  case 'LAYTABLE_TIPS':
			  layer.alert('这是工具栏右侧自定义的一个图标按钮');
		  break;
		  };
	  });
	  
	  //监听行工具事件
	  table.on('tool(test)',function(obj){
		  var data=obj.data;
		  if(obj.event==='del'){
			  layer.confirm('确定删除吗？',function(index){
				// obj.del();
				 layer.close(index);
					 
				  //向服务器端发送删除指令
				  $.ajax({
					  url:'/delTalent?id='+data.id,
					  method:'post',
					  async:false,
					  
					  success:function(data){
						  if(data){
						  layer.msg("删除成功");
						  obj.del();
						  } 
						//  }else{layer.msg("删除失败");}
					  },
					  error:function(data){
						  if(data){
							  layer.msg("删除失败");
						  }
					  }
				  });
			  });
		  }else if(obj.event==='edit'){
			 layui.use('form',function(){
				 var form=layui.form;
				 var $=layui.jquery;
			 var editIndex=layer.open({
				  formType:1,
				  title:'修改信息',
				  btn:['确认','取消'],
				  yes:function(editIndex,layero){
					//获取表单的值，必须再次获取，若在给弹窗赋值后再获取，则获取到的仍是原始数据，相当于加载了一个空的表单，然后弹窗赋值，然后获取表单的值
					 var id=$("#id").val();
				     var name=$("#name").val();
				     var age=$("#age").val();
				     var huntPosition=$("#huntPosition").val();
				     var askSlary=$("#askSlary").val();
				     var skill=$("#skill").val();
				     var interviewTime=$("#interviewTime").val();
					 // layer.msg(account);

					  //向后台发送修改请求
					  $.ajax({
						  url:'/editTalent',
						  contentType:"application/json;charset=utf-8",
						  method:'post',
						  data:JSON.stringify({id:id,name:name,age:age,huntPosition:huntPosition,askSlary:askSlary,skill:skill,interviewTime:interviewTime}),
						  success:function(data){
							  if(data){
							  layer.msg("修改成功");
							  obj.update({
								  name:name,
								  age:age,
								  huntPosition:huntPosition,
								  askSlary:askSlary,
								  skill:skill,
								  interviewTime:interviewTime
							  });
						  }else{layer.msg("修改失败");} 
						  },
						  error:function(data){layer.msg("修改失败");}
					  });
				  },
				  btn2:function(editIndex,layero){
					  layer.close(editIndex);
				  },
				//  end:function(){parent.reload();},
				  area:['450px','450px'],
				  content:$('#editLayer').html(),
				  success:function(layero,index){ 
					  //给个控件赋值
						 $("[name='id']").val(data.id);
						 $("[name='name']").val(data.name);
						 $("[name='age']").val(data.age);
						 $("[name='huntPosition']").val(data.huntPosition);
						 $("[name='askSlary']").val(data.askSlary);
						 $("[name='skill']").val(data.skill);
						 $("[name='interviewTime']").val(data.interviewTime);
						 form.render()},
				  });			
			      
			
			 
			 });
		  }
	  });
  });
  

  
</script>
   
</body>
</html>